import { defineStore } from 'pinia';
import { localStorage } from '@/utils/StorageUtils.ts';

/**
 * 应用状态管理
 * @module ApplicationStore
 * @description 管理应用的全局状态，包括主题、语言信息等
 */

export const useApplicationStore = defineStore('application-store', {
    state: () => ({
        // 是否为深色主题
        darkTheme: false,
        // 当前系统主题色
        appTheme: '#2d8cf0',
        // 当前系统背景颜色
        appBackgroundColor: '#ffffff',
        // 当前系统文字颜色
        appTextColor: '#ffffff',
        // 当前系统语言
        appLanguage: 'zh-CN',
        // 是否展开侧边栏
        expanded: true,
    }),
    getters: {
        getDarkTheme(): boolean {
            this.darkTheme = localStorage.get('theme.darkTheme', false);
            return this.darkTheme;
        },
        getAppTheme(): string {
            this.appTheme = localStorage.get('theme.appTheme', '#2d8cf0');
            return this.appTheme;
        },
        getAppBackgroundColor(): string {
            this.appBackgroundColor = localStorage.get('theme.appBackgroundColor', '#ffffff');
            return this.appBackgroundColor;
        },
        getAppTextColor(): string {
            this.appTextColor = localStorage.get('theme.appTextColor', '#000000');
            return this.appTextColor;
        },
        getAppLanguage(): string {
            this.appLanguage = localStorage.get('theme.appLanguage', 'zh-CN');
            return this.appLanguage;
        },
        getExpanded(): boolean {
            this.expanded = localStorage.get('theme.expanded', false);
            return this.expanded;
        },
    },
    actions: {
        setDarkTheme(val: boolean) {
            localStorage.set('theme.darkTheme', val);
            this.darkTheme = val;
        },
        setAppTheme(val: string) {
            localStorage.set('theme.appTheme', val);
            this.appTheme = val;
        },
        setAppBackgroundColor(val: string) {
            localStorage.set('theme.appBackgroundColor', val);
            document.documentElement.style.setProperty("--theme-background-color", val);
            this.appBackgroundColor = val;
        },
        setAppTextColor(val: string) {
            localStorage.set('theme.appTextColor', val);
            document.documentElement.style.setProperty("--theme-text-color", val);
            this.appTextColor = val;
        },
        setAppLanguage(val: string) {
            localStorage.set('theme.appLanguage', val);
            this.appLanguage = val;
        },
        setExpanded(expanded: boolean) {
            localStorage.set('theme.expanded', expanded);
            this.expanded = expanded;
        },
        initialize() {
            document.documentElement.style.setProperty("--theme-background-color", this.getAppBackgroundColor);
            document.documentElement.style.setProperty("--theme-text-color", this.getAppTextColor);
        }
    }
});
